//
//  UIViewController+Swilzzling.m
//  SDemo
//
//  Created by gg on 2018/7/17.
//  Copyright © 2018年 gg. All rights reserved.
//

#import "UIViewController+Swilzzling.h"
#import "NSObject+SHMethodSwizzle.h"
#import <objc/message.h>
@implementation UIViewController (Swilzzling)

/**
 load方法: 把类加载进内存的时候调用,只会调用一次 (引用类头文件的时候就会被调用)
 方法应先交换，再去调用
 */
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //Class class = [self class];
        [self intanceMethodExchangeWithOriginSelector:@selector(viewWillAppear:) swizzledSelector:@selector(sh_viewWillAppear:)];
        
//        // When swizzling a class method, use the following:
//        // Class class = object_getClass((id)self);
//        SEL originalSelector = @selector(viewWillAppear:);
//        SEL swizzledSelector = @selector(sh_viewWillAppear:);
//        Method originalMethod = class_getInstanceMethod(class, originalSelector);
//        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
//        BOOL didAddMethod = class_addMethod(class,
//                                            originalSelector,
//                                            method_getImplementation(swizzledMethod),
//                                            method_getTypeEncoding(swizzledMethod));
//        if (didAddMethod) {
//            class_replaceMethod(class,
//                                swizzledSelector,
//                                method_getImplementation(originalMethod),
//                                method_getTypeEncoding(originalMethod));
//        } else {
//            method_exchangeImplementations(originalMethod, swizzledMethod);
//        }
//        
    });
}

#pragma mark - Method Swizzling
- (void)sh_viewWillAppear:(BOOL)animated {
    [self sh_viewWillAppear:animated];
    NSLog(@"viewWillAppear: %@", self);
}


@end
